import {
  Context,
  Controller,
  Files,
  Inject,
  Post,
} from '@midwayjs/core';
import { ApiBody, ApiResponse, ApiTags, BodyContentType } from '@midwayjs/swagger';
import { Domain, FilesDto, UploadDto, UploadResDto } from '../model';
import { ServerResponse } from '../util/response';
import { Config } from '@midwayjs/core';

@ApiTags(['公共'])
@Controller('/common')
export class CommonController {
  @Inject()
  ctx: Context;
  @Config('domain')
  domain:Domain;
  @Post('/upload', { summary: '文件上传' })
  @ApiBody({
    contentType: BodyContentType.Multipart,
    schema: {
      type: UploadDto,
    },
  })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: UploadResDto,
  })
  async upload(@Files() files: FilesDto[]) {
    if (files.length === 0) {
      return ServerResponse.JSON_TPL({}, false,'上传失败');
    }
    const data = `${this.domain.url}/public${files[0].data.split('/public')[1]}`
    return ServerResponse.JSON_TPL({
      url:data
    },true,'上传成功');
  }
}


